/* ***********************************************************************
 * 项目名称:油耗
 * 项目组:
 * 创建时间:2017.7.12
 * 创建人:马方印
 * 用途:车辆实时状态
 * 版本:1.0
 * 修改记录:    修改内容   修改原因   修改人   修改时间
 *               无        无       无       无
 *************************************************************************/

#define kWorkDetailCellHeight 44
#define kHeaderViewHeight 45
#define kFooterViewHeight 75
#define kStatusBarAndNavigationBarHeight 64
#define kSwitchViewHeight 50
#define kWorkAreaDetailTableViewHeight (kScreenHeight - kHeaderViewHeight - kFooterViewHeight - kStatusBarAndNavigationBarHeight - kSwitchViewHeight)

#import "FCVehicleReamTimeStatusController.h"
#import "FCSwitchView.h"
#import "HomeCarModel.h"
#import "FCVehicleReamTimeStatusHeaderView.h"
#import "FCVehicleReamTimeStatusFooterView.h"
#import "FCVehicleReamTimeStatusCell.h"
#import "MFYAnimationManager.h"//页面跳转动画
#import "FCVehicleRealTimeStatueDetailView.h"//详情显示
#import "FCVehicleIolAndCurveController.h"//车辆油耗与速度曲线页面
#import "FCVehicleReamTimeStatusModel.h"

@interface FCVehicleReamTimeStatusController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *workDetailTableView;//工作明细列表

@property (nonatomic,strong) FCVehicleReamTimeStatusHeaderView *headerView;//区头视图

@property (nonatomic,strong) FCVehicleReamTimeStatusFooterView *footerView;//区尾视图

@property (nonatomic, strong) FCVehicleRealTimeStatueDetailView *detailView;

@property (nonatomic, strong) NSMutableArray *carListArray;

@property (nonatomic, copy) NSString *currentType;//0离线 1在线

@end

static NSString *identifier = @"vehicReamTimeStatusCell";

@implementation FCVehicleReamTimeStatusController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"车辆实时状态";
    
    [self titleView];
    [self setUpSwitchViewAppeareace];
    [self setUpWorkAreaDetailHeaderView];
    [self setUpWorkAreaDetailFooterView];
    
    [self.view addSubview:self.workDetailTableView];
    [self.view addSubview:self.detailView];
    
    
}

#pragma mark 设置选项卡
-(void)setUpSwitchViewAppeareace {
    
    __weak typeof(self) weakSelf = self;
    
    //设置在线离线显示数量
    NSString *nowCar = [NSString stringWithFormat:@"在线车辆(%@)",self.carModel.nowCar];
    NSString *leaveCar = [NSString stringWithFormat:@"离线车辆(%@)",self.carModel.leaveCar];
    [self.switchView setUpSwitchViewTitleWith:@[nowCar,leaveCar]];
    [self.switchView.firstChooseBtn setBackgroundColor:COLOR(24, 134, 200, 1)];
    
    //设置默认选中
    [self.switchView setUpSwitchButtonAppreace:self.type];
    
    if (self.type == 0) {
        
        [self.headerView.timeLabel setTitle:@"在线时间" forState:UIControlStateNormal];
        
        _currentType = @"0";
        
        [self getNowLeaveNotesWithType:_currentType];
    }
    else{
        
        [self.headerView.timeLabel setTitle:@"离线时间" forState:UIControlStateNormal];
        
        _currentType = @"1";
        
        [self getNowLeaveNotesWithType:_currentType];
    }
    
    self.switchView.switchActionBlock = ^(NSInteger tag){
        
        NSInteger index = tag - 100;
        
        if (index == 0) {
            
            [weakSelf.headerView.timeLabel setTitle:@"在线时间" forState:UIControlStateNormal];
        }
        else{
            
            [weakSelf.headerView.timeLabel setTitle:@"离线时间" forState:UIControlStateNormal];
        }
        
        _currentType = [NSString stringWithFormat:@"%ld",index];
        
        [weakSelf getNowLeaveNotesWithType:weakSelf.currentType];
    };
}

#pragma mark 数据请求
- (void)getNowLeaveNotesWithType:(NSString *)type{
    
    NSDictionary *params = @{
                             
                             @"type" : type,
                             @"departIDs": @""
                             
                             };
    
    [MBProgressHUD postLoadViewWithText:HUD_PLEASEWAIT View:kWindow];
    
    [ClientRequest SOAPMthod:@"nowLeaveNotes" soapBody:params success:^(id responseObject) {
        
        [MBProgressHUD hideLoadViewWithView:kWindow];
        
        [self.carListArray removeAllObjects];
        
        for (NSDictionary *info in responseObject[@"carList"]) {
            
            FCVehicleReamTimeStatusModel *model = [FCVehicleReamTimeStatusModel mj_objectWithKeyValues:info];
            
            [self.carListArray addObject:model];
        }
        
        [self.workDetailTableView reloadData];
        
    } failure:^(NSError *error) {
       
        [MBProgressHUD postMessageWithText:@"请求失败" View:kWindow];
        
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器的错误原因：%@",str);
    }];
}

- (void)setUpWorkAreaDetailHeaderView {
    
    self.headerView = [[NSBundle mainBundle] loadNibNamed:@"FCVehicleReamTimeStatusHeaderView" owner:self options:nil].firstObject;
    self.headerView.frame = CGRectMake(0, CGRectGetMaxY(self.switchView.frame), kScreenWidth, 45);
    
    if (self.type == 0) {
        
        [self.headerView.timeLabel setTitle:@"在线时间" forState:UIControlStateNormal];
    }
    else{
        
        [self.headerView.timeLabel setTitle:@"离线时间" forState:UIControlStateNormal];
    }
    
    [self.view addSubview:self.headerView];
}

- (void)setUpWorkAreaDetailFooterView {
    
    self.footerView  = [[NSBundle mainBundle] loadNibNamed:@"FCVehicleReamTimeStatusFooterView" owner:self options:nil].firstObject;
    self.footerView.frame = CGRectMake(0, kScreenHeight - kFooterViewHeight, kScreenWidth, kFooterViewHeight);
    
    __weak typeof(self) weakSelf = self;
    
    self.footerView.popToControllerCompletionHandle = ^(){
        //车辆油耗与速度曲线页面
        FCVehicleIolAndCurveController *vehicleIolAndCurveVC = [[FCVehicleIolAndCurveController alloc]init];
        [weakSelf.navigationController pushViewController:vehicleIolAndCurveVC animated:YES];
    };
    
    [self.view addSubview:self.footerView];
}

#pragma mark - tableView 代码
#pragma mark - UITableView delegate and datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.carListArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    FCVehicleReamTimeStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    FCVehicleReamTimeStatusModel *model = self.carListArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FCVehicleReamTimeStatusModel *model = self.carListArray[indexPath.row];
    
    self.detailView.model = model;
    
    [self.detailView setHidden:NO];
}

#pragma mark - 懒加载
- (FCVehicleRealTimeStatueDetailView *)detailView{
    
    if (!_detailView) {
        
        _detailView = [[FCVehicleRealTimeStatueDetailView alloc]init];
        _detailView.frame = [[UIScreen mainScreen] bounds];
        _detailView.viewController = self;
        [_detailView setHidden:YES];
    }
    
    return _detailView;
}

- (UITableView *)workDetailTableView {
    
    if (!_workDetailTableView) {
        
        _workDetailTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headerView.frame), kScreenWidth, kWorkAreaDetailTableViewHeight) style: UITableViewStylePlain];
        
        _workDetailTableView.delegate = self;
        _workDetailTableView.dataSource = self;
        _workDetailTableView.rowHeight = kWorkDetailCellHeight;
        _workDetailTableView.bounces = NO;
        _workDetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _workDetailTableView.showsVerticalScrollIndicator = NO;
        
        [_workDetailTableView registerNib:[UINib nibWithNibName:@"FCVehicleReamTimeStatusCell" bundle:nil] forCellReuseIdentifier:identifier];
    }
    
    return _workDetailTableView;
}

- (NSMutableArray *)carListArray{
    
    if (!_carListArray) {
        
        _carListArray = [NSMutableArray array];
    }
    
    return _carListArray;
}

#pragma mark - 重写导航条左右按钮
- (void)titleView{
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"车辆实时状态";
    label.bounds = CGRectMake(0, 0, 100, 44);
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
    
    // 下个视图的返回按钮
    UIBarButtonItem* returnButtonItem = [[UIBarButtonItem alloc] init];
    returnButtonItem.title = @"";
    self.navigationItem.backBarButtonItem = returnButtonItem;
}

//leftButtonItem
- (UIView *)leftButtonItem {
    
    UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    left.frame = CGRectMake(0, 0, 30, 30);
    [left setBackgroundImage:[UIImage imageNamed:@"leftArrow"] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(leftButtonItemAction) forControlEvents:UIControlEventTouchUpInside];
    return left;
}

//rightButtonItem
- (UIView *)rightButtonItem {
    
    return nil;
}

//leftButtonItemAction
- (void)leftButtonItemAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
